

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=auto>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/img/fluid.png">
  <link rel="icon" href="/img/fluid.png">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="author" content="John Doe">
  <meta name="keywords" content="">
  
    <meta name="description" content="@[toc] 以往课设记录  Web期末课程设计v1.0（一） | 大学生闲置物品交易系统 | 选择页面模板（附资源），使用 JQuery AJAX实现注册、登陆  Web期末课程设计v1.0（二） | 闲优购 | 使用Bootstrap5各组件优化页面，使用AJAX请求实现用户修改信息功能  Web期末课程设计v1.0（三） | 闲优购系统 | 基于Bootstrap设计商品页面、使用JQuer">
<meta property="og:type" content="article">
<meta property="og:title" content="Web项目实战 _ 购物系统v1.0 _ 开发记录（四） _ 实现JQuery图片上传 _ 使用JQ插件提交AJAX请求实现发布商品并保存到数据库 _ 商品分类分页显示">
<meta property="og:url" content="http://example.com/2021/12/23/Web%E9%A1%B9%E7%9B%AE%E5%AE%9E%E6%88%98%20_%20%E8%B4%AD%E7%89%A9%E7%B3%BB%E7%BB%9Fv1.0%20_%20%E5%BC%80%E5%8F%91%E8%AE%B0%E5%BD%95%EF%BC%88%E5%9B%9B%EF%BC%89%20_%20%E5%AE%9E%E7%8E%B0JQuery%E5%9B%BE%E7%89%87%E4%B8%8A%E4%BC%A0%20_%20%E4%BD%BF%E7%94%A8JQ%E6%8F%92%E4%BB%B6%E6%8F%90%E4%BA%A4AJAX%E8%AF%B7%E6%B1%82%E5%AE%9E%E7%8E%B0%E5%8F%91%E5%B8%83%E5%95%86%E5%93%81%E5%B9%B6%E4%BF%9D%E5%AD%98%E5%88%B0%E6%95%B0%E6%8D%AE%E5%BA%93%20_%20%E5%95%86%E5%93%81%E5%88%86%E7%B1%BB%E5%88%86%E9%A1%B5%E6%98%BE%E7%A4%BA/index.html">
<meta property="og:site_name" content="Hexo">
<meta property="og:description" content="@[toc] 以往课设记录  Web期末课程设计v1.0（一） | 大学生闲置物品交易系统 | 选择页面模板（附资源），使用 JQuery AJAX实现注册、登陆  Web期末课程设计v1.0（二） | 闲优购 | 使用Bootstrap5各组件优化页面，使用AJAX请求实现用户修改信息功能  Web期末课程设计v1.0（三） | 闲优购系统 | 基于Bootstrap设计商品页面、使用JQuer">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://img-blog.csdnimg.cn/705ef10623c840459742e41014c13e15.png#pic_center">
<meta property="og:image" content="https://img-blog.csdnimg.cn/f8adb6cdc16e4178b561a1f4a5439ac0.png#pic_center">
<meta property="og:image" content="https://img-blog.csdnimg.cn/9016d2efcd59467099cddea27e91a961.png#pic_center">
<meta property="og:image" content="https://img-blog.csdnimg.cn/a11f0b5e3ad04fef97654f4d208a5e10.png#pic_center">
<meta property="og:image" content="https://img-blog.csdnimg.cn/fa7ef53c39cb48e5b0134345089d8402.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/c059c780686d4ff08f196edc0138f2b1.png#pic_center">
<meta property="og:image" content="https://img-blog.csdnimg.cn/472209cb44314927a567dcf97f827620.png#pic_center">
<meta property="og:image" content="https://img-blog.csdnimg.cn/0d05582ecb264337b873ed5dc2fa49f1.png#pic_center">
<meta property="og:image" content="https://img-blog.csdnimg.cn/732489de5b8046368152960291c93fd1.png#pic_center">
<meta property="og:image" content="https://img-blog.csdnimg.cn/6c328aad9fde456cbcb4c3ff6de55316.png#pic_center">
<meta property="og:image" content="https://img-blog.csdnimg.cn/cdef32be35794a5ea3faf5f173fdf300.png#pic_center">
<meta property="og:image" content="https://img-blog.csdnimg.cn/ec8a849dbb524da4b2ca1d3501354a7a.png#pic_center">
<meta property="og:image" content="https://img-blog.csdnimg.cn/ade900ca37844e1fa53c878e56c0f8ff.png">
<meta property="article:published_time" content="2021-12-23T02:12:59.000Z">
<meta property="article:modified_time" content="2022-08-22T15:50:30.414Z">
<meta property="article:author" content="John Doe">
<meta property="article:tag" content="JavaWeb">
<meta property="article:tag" content="项目实战:购物系统v1.0">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:image" content="https://img-blog.csdnimg.cn/705ef10623c840459742e41014c13e15.png#pic_center">
  
  
  
  <title>Web项目实战 _ 购物系统v1.0 _ 开发记录（四） _ 实现JQuery图片上传 _ 使用JQ插件提交AJAX请求实现发布商品并保存到数据库 _ 商品分类分页显示 - Hexo</title>

  <link  rel="stylesheet" href="https://lib.baomitu.com/twitter-bootstrap/4.6.1/css/bootstrap.min.css" />



  <link  rel="stylesheet" href="https://lib.baomitu.com/github-markdown-css/4.0.0/github-markdown.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/hint.css/2.7.0/hint.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.css" />



<!-- 主题依赖的图标库，不要自行修改 -->
<!-- Do not modify the link that theme dependent icons -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_hj8rtnfg7um.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_lbnruvf0jn.css">


<link  rel="stylesheet" href="/css/main.css" />


  <link id="highlight-css" rel="stylesheet" href="/css/highlight.css" />
  
    <link id="highlight-css-dark" rel="stylesheet" href="/css/highlight-dark.css" />
  




  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    Fluid.ctx = Object.assign({}, Fluid.ctx)
    var CONFIG = {"hostname":"example.com","root":"/","version":"1.9.2","typing":{"enable":true,"typeSpeed":70,"cursorChar":"_","loop":false,"scope":[]},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"left","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"code_language":{"enable":true,"default":"TEXT"},"copy_btn":true,"image_caption":{"enable":true},"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"placement":"right","headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":0},"lazyload":{"enable":true,"loading_img":"/img/loading.gif","onlypost":false,"offset_factor":2},"web_analytics":{"enable":false,"follow_dnt":true,"baidu":null,"google":null,"gtag":null,"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":null,"app_key":null,"server_url":null,"path":"window.location.pathname","ignore_local":false}},"search_path":"/local-search.xml"};

    if (CONFIG.web_analytics.follow_dnt) {
      var dntVal = navigator.doNotTrack || window.doNotTrack || navigator.msDoNotTrack;
      Fluid.ctx.dnt = dntVal && (dntVal.startsWith('1') || dntVal.startsWith('yes') || dntVal.startsWith('on'));
    }
  </script>
  <script  src="/js/utils.js" ></script>
  <script  src="/js/color-schema.js" ></script>
  


  
<meta name="generator" content="Hexo 6.2.0"></head>


<body>
  

  <header>
    

<div class="header-inner" style="height: 70vh;">
  <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand" href="/">
      <strong>尤 Ni&#39;s Blog</strong>
    </a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/">
                <i class="iconfont icon-home-fill"></i>
                首页
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/archives/">
                <i class="iconfont icon-archive-fill"></i>
                归档
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/categories/">
                <i class="iconfont icon-category-fill"></i>
                分类
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/tags/">
                <i class="iconfont icon-tags-fill"></i>
                标签
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/about/">
                <i class="iconfont icon-user-fill"></i>
                关于
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" href="javascript:;" data-toggle="modal" data-target="#modalSearch" aria-label="Search">
              &nbsp;<i class="iconfont icon-search"></i>&nbsp;
            </a>
          </li>
          
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self" href="javascript:;" aria-label="Color Toggle">&nbsp;<i
                class="iconfont icon-dark" id="color-toggle-icon"></i>&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

  

<div id="banner" class="banner" parallax=true
     style="background: url('/img/default.png') no-repeat center center; background-size: cover;">
  <div class="full-bg-img">
    <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
      <div class="banner-text text-center fade-in-up">
        <div class="h2">
          
            <span id="subtitle" data-typed-text="Web项目实战 _ 购物系统v1.0 _ 开发记录（四） _ 实现JQuery图片上传 _ 使用JQ插件提交AJAX请求实现发布商品并保存到数据库 _ 商品分类分页显示"></span>
          
        </div>

        
          
  <div class="mt-3">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-author" aria-hidden="true"></i>
        John Doe
      </span>
    
    
      <span class="post-meta">
        <i class="iconfont icon-date-fill" aria-hidden="true"></i>
        <time datetime="2021-12-23 10:12" pubdate>
          2021年12月23日 上午
        </time>
      </span>
    
  </div>

  <div class="mt-1">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-chart"></i>
        
          13k 字
        
      </span>
    

    
      <span class="post-meta mr-2">
        <i class="iconfont icon-clock-fill"></i>
        
        
        
          107 分钟
        
      </span>
    

    
    
  </div>


        
      </div>

      
    </div>
  </div>
</div>

</div>

  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="side-col d-none d-lg-block col-lg-2">
      

    </div>

    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div id="board">
          <article class="post-content mx-auto">
            <!-- SEO header -->
            <h1 style="display: none">Web项目实战 _ 购物系统v1.0 _ 开发记录（四） _ 实现JQuery图片上传 _ 使用JQ插件提交AJAX请求实现发布商品并保存到数据库 _ 商品分类分页显示</h1>
            
              <p class="note note-info">
                
                  
                    本文最后更新于：1 小时前
                  
                
              </p>
            
            
              <div class="markdown-body">
                
                <p>@[toc]</p>
<h1 id="以往课设记录"><a href="#以往课设记录" class="headerlink" title="以往课设记录"></a>以往课设记录</h1><hr>
<ul>
<li><p><a target="_blank" rel="noopener" href="https://blog.csdn.net/Unirithe/article/details/122028999">Web期末课程设计v1.0（一） | 大学生闲置物品交易系统 | 选择页面模板（附资源），使用 JQuery AJAX实现注册、登陆</a></p>
</li>
<li><p><a target="_blank" rel="noopener" href="https://blog.csdn.net/Unirithe/article/details/122035714">Web期末课程设计v1.0（二） | 闲优购 | 使用Bootstrap5各组件优化页面，使用AJAX请求实现用户修改信息功能</a></p>
</li>
<li><p><a target="_blank" rel="noopener" href="https://blog.csdn.net/Unirithe/article/details/122055272">Web期末课程设计v1.0（三） | 闲优购系统 | 基于Bootstrap设计商品页面、使用JQuery实现上传文件并显示到图片组件、使用JSTL标签显示数据库的内容到无序列表</a></p>
</li>
</ul>
<p>由于遇到了瓶颈…与以往每天一次的总结不同，这次的记录是笔者<strong>两天</strong>的总结。</p>
<h1 id="运行环境"><a href="#运行环境" class="headerlink" title="运行环境"></a>运行环境</h1><hr>
<ul>
<li>windows10</li>
<li>IDEA专业版</li>
<li>JDK8</li>
<li>HTML5、CSS3、JavaScript</li>
<li>JQuery3.6.0</li>
<li>Bootstrap5</li>
</ul>
<h1 id="1-页面效果展示"><a href="#1-页面效果展示" class="headerlink" title="1. 页面效果展示"></a>1. 页面效果展示</h1><hr>
<h2 id="1-1-首页"><a href="#1-1-首页" class="headerlink" title="1.1 首页"></a>1.1 首页</h2><p>不同于之前的首页样式，笔者这里设计了一个简略的首页…<br><img src="https://img-blog.csdnimg.cn/705ef10623c840459742e41014c13e15.png#pic_center" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<h2 id="1-2-发布商品页面"><a href="#1-2-发布商品页面" class="headerlink" title="1.2 发布商品页面"></a>1.2 发布商品页面</h2><p>发布商品首先是通过提交表单到servlet，然后servlet处理这个请求，把表单的信息包括图片名称、描述、类型、价格、图片地址存储到数据库，最后才返回。<br><img src="https://img-blog.csdnimg.cn/f8adb6cdc16e4178b561a1f4a5439ac0.png#pic_center" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<h2 id="1-3-商品中心页面"><a href="#1-3-商品中心页面" class="headerlink" title="1.3 商品中心页面"></a>1.3 商品中心页面</h2><p>这些显示的商品都是从数据库里读取而来的，每次访问页面都会读取数据库对应的商品数据，但为了实现分页，笔者用了较为复杂的逻辑来处理。<br><img src="https://img-blog.csdnimg.cn/9016d2efcd59467099cddea27e91a961.png#pic_center" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"><img src="https://img-blog.csdnimg.cn/a11f0b5e3ad04fef97654f4d208a5e10.png#pic_center" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<h2 id="1-4-购买窗口"><a href="#1-4-购买窗口" class="headerlink" title="1.4 购买窗口"></a>1.4 购买窗口</h2><p>购买窗口只是一个简单的确认提示框。<br><img src="https://img-blog.csdnimg.cn/fa7ef53c39cb48e5b0134345089d8402.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<h1 id="2-功能实现"><a href="#2-功能实现" class="headerlink" title="2. 功能实现"></a>2. 功能实现</h1><hr>
<h2 id="2-1-实现分页查询"><a href="#2-1-实现分页查询" class="headerlink" title="2.1 实现分页查询"></a>2.1 实现分页查询</h2><h3 id="2-1-1-分页原理——mysql的指定查询"><a href="#2-1-1-分页原理——mysql的指定查询" class="headerlink" title="2.1.1 分页原理——mysql的指定查询"></a>2.1.1 分页原理——mysql的指定查询</h3><p>一般的Mysql查表语句为：<code>select * from 表名;</code> 效果如下:<br><img src="https://img-blog.csdnimg.cn/c059c780686d4ff08f196edc0138f2b1.png#pic_center" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"><br>如果要根据主键查询前n个数据，则为 <code>select * from 表名 limit n</code> 效果如下:<br><img src="https://img-blog.csdnimg.cn/472209cb44314927a567dcf97f827620.png#pic_center" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"><br>同理，若要查询 m 后的n个数据，则为 <code>select * from 表名 limit m, n</code> 效果如下:<br><img src="https://img-blog.csdnimg.cn/0d05582ecb264337b873ed5dc2fa49f1.png#pic_center" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"><br>综上，如果要实现分页查询，只要先确定每一页展示多少个商品，比如笔者这里是展示8个商品。</p>
<p>然后使用一个page对象表示当前的页面信息，比如当前的页数，如果是第1页。</p>
<p>那么在访问JSP页面时，servlet类里查询的语句为： <code>select * from commodity limit 1, 8</code> </p>
<p>以此类推，第n页的查询语句为: <code>select * from commodity limit n, 8</code></p>
<h3 id="2-1-2-存储分页信息的方式"><a href="#2-1-2-存储分页信息的方式" class="headerlink" title="2.1.2 存储分页信息的方式"></a>2.1.2 存储分页信息的方式</h3><p>基于本学期的web学习，笔者的思路是: </p>
<ol>
<li><p>设计一个page对象，里面有当前页数、每页显示条数、总页数、总记录条数这四个属性</p>
</li>
<li><p>前端页面JSP只需要获取session域的page对象就可以获取分页查询的一些关键要素</p>
</li>
</ol>
<p>这主要用于显示当前页数，然后在servlet类中同样能使用session域的page对象进行查询操作，查询指定的商品数据，最终将查询结果存储到session域。</p>
<p><code>Page.java</code></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">package</span> com.uni.entity;<br><br><span class="hljs-keyword">import</span> lombok.AllArgsConstructor;<br><span class="hljs-keyword">import</span> lombok.Data;<br><span class="hljs-keyword">import</span> lombok.NoArgsConstructor;<br><br><span class="hljs-meta">@NoArgsConstructor</span><br><span class="hljs-meta">@AllArgsConstructor</span><br><span class="hljs-meta">@Data</span><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">Page</span> &#123;<br>    <span class="hljs-keyword">private</span> <span class="hljs-type">int</span> pageNow;        <span class="hljs-comment">// 当前页数</span><br>    <span class="hljs-keyword">private</span> <span class="hljs-type">int</span> pageCount;      <span class="hljs-comment">// 每页显示条数</span><br>    <span class="hljs-keyword">private</span> <span class="hljs-type">int</span> totalPage;      <span class="hljs-comment">// 总页数</span><br>    <span class="hljs-keyword">private</span> <span class="hljs-type">int</span> totalRows;      <span class="hljs-comment">// 总记录</span><br>&#125;<br></code></pre></td></tr></table></figure>

<h3 id="2-1-3-实现分页信息变更的Servlet类"><a href="#2-1-3-实现分页信息变更的Servlet类" class="headerlink" title="2.1.3 实现分页信息变更的Servlet类"></a>2.1.3 实现分页信息变更的Servlet类</h3><p>这个servlet类是在切换页面时调用的，它的作用仅仅是根据前端的选择来更新page对象的值即修改当前页面的值。</p>
<p>考虑到切换页面有两种方式</p>
<ol>
<li>直接上一页或者下一页切换页面</li>
<li>访问具体的页数</li>
</ol>
<p>所以需进行不同情况的判断</p>
<p>最终请求转发到的 <code>shop_center</code> 位置是做查询工作，首次访问商品中心就需要通过这个地址。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">package</span> com.uni.action;<br><br><span class="hljs-keyword">import</span> com.uni.entity.Page;<br><span class="hljs-keyword">import</span> javax.servlet.ServletException;<br><span class="hljs-keyword">import</span> javax.servlet.annotation.WebServlet;<br><span class="hljs-keyword">import</span> javax.servlet.http.HttpServlet;<br><span class="hljs-keyword">import</span> javax.servlet.http.HttpServletRequest;<br><span class="hljs-keyword">import</span> javax.servlet.http.HttpServletResponse;<br><span class="hljs-keyword">import</span> javax.servlet.http.HttpSession;<br><span class="hljs-keyword">import</span> java.io.IOException;<br><br><span class="hljs-meta">@WebServlet(name = &quot;PageAction&quot;, urlPatterns = &quot;/shop&quot;)</span><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">PageAction</span> <span class="hljs-keyword">extends</span> <span class="hljs-title class_">HttpServlet</span> &#123;<br>    <span class="hljs-meta">@Override</span><br>    <span class="hljs-keyword">protected</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">doGet</span><span class="hljs-params">(HttpServletRequest req, HttpServletResponse resp)</span> <span class="hljs-keyword">throws</span> ServletException, IOException &#123;<br>        <span class="hljs-type">String</span> <span class="hljs-variable">newPage</span> <span class="hljs-operator">=</span> req.getParameter(<span class="hljs-string">&quot;page&quot;</span>);<br>        <span class="hljs-type">HttpSession</span> <span class="hljs-variable">session</span> <span class="hljs-operator">=</span> req.getSession();<br>        <span class="hljs-type">Page</span> <span class="hljs-variable">page</span> <span class="hljs-operator">=</span> (Page) session.getAttribute(<span class="hljs-string">&quot;pageInfo&quot;</span>);<br>        <span class="hljs-type">int</span> <span class="hljs-variable">nowPage</span> <span class="hljs-operator">=</span> page.getPageNow();<br>        <span class="hljs-keyword">switch</span> (newPage) &#123;<br>            <span class="hljs-keyword">case</span> <span class="hljs-string">&quot;last&quot;</span>:<br>                page.setPageNow(Integer.max(<span class="hljs-number">0</span>, nowPage-<span class="hljs-number">1</span>));<br>                <span class="hljs-keyword">break</span>;<br>            <span class="hljs-keyword">case</span> <span class="hljs-string">&quot;next&quot;</span>:<br>                page.setPageNow((nowPage + <span class="hljs-number">1</span>) % (page.getTotalPage() + <span class="hljs-number">1</span>));<br>                <span class="hljs-keyword">break</span>;<br>            <span class="hljs-keyword">default</span>:<br>                <span class="hljs-type">int</span> <span class="hljs-variable">i</span> <span class="hljs-operator">=</span> Integer.parseInt(newPage) - <span class="hljs-number">1</span>;<br>                <span class="hljs-keyword">if</span>(i &gt;= <span class="hljs-number">0</span> &amp;&amp; i &lt;= page.getTotalPage())<br>                    page.setPageNow(i);<br>                <span class="hljs-keyword">break</span>;<br>        &#125;<br>        session.setAttribute(<span class="hljs-string">&quot;pageInfo&quot;</span>, page);<br>        req.getRequestDispatcher(<span class="hljs-string">&quot;/shop_center&quot;</span>).forward(req, resp);<br>    &#125;<br>&#125;<br><br></code></pre></td></tr></table></figure>

<h3 id="2-1-4-实现分页查询的Servlet类"><a href="#2-1-4-实现分页查询的Servlet类" class="headerlink" title="2.1.4 实现分页查询的Servlet类"></a>2.1.4 实现分页查询的Servlet类</h3><p>在分页查询时，所考虑的问题比较多，比如：</p>
<ul>
<li>首次访问时，session域没有page对象，所以当page为空时，需要进行初始化。</li>
<li>分页查询需考虑当前的商品类型，因为用于既可能只查看一类商品，也可能查看所有的商品（查询所有数据，笔者使用-1来表示），类似于推荐页面。</li>
<li>分页查询有两种情况：</li>
</ul>
<ol>
<li>用户通过商品类别标签而选择商品信息，这时需要对商品的当前页面进行初始化<br><img src="https://img-blog.csdnimg.cn/732489de5b8046368152960291c93fd1.png#pic_center" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></li>
<li>通过切换页面访问<br><img src="https://img-blog.csdnimg.cn/6c328aad9fde456cbcb4c3ff6de55316.png#pic_center" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></li>
</ol>
<p><code>CommodityQueryAction.java</code></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">package</span> com.uni.action;<br><br><span class="hljs-keyword">import</span> com.uni.entity.Commodity;<br><span class="hljs-keyword">import</span> com.uni.entity.CommodityType;<br><span class="hljs-keyword">import</span> com.uni.entity.Page;<br><span class="hljs-keyword">import</span> com.uni.service.Service;<br><br><span class="hljs-keyword">import</span> javax.servlet.ServletException;<br><span class="hljs-keyword">import</span> javax.servlet.annotation.WebServlet;<br><span class="hljs-keyword">import</span> javax.servlet.http.HttpServlet;<br><span class="hljs-keyword">import</span> javax.servlet.http.HttpServletRequest;<br><span class="hljs-keyword">import</span> javax.servlet.http.HttpServletResponse;<br><span class="hljs-keyword">import</span> javax.servlet.http.HttpSession;<br><span class="hljs-keyword">import</span> java.io.IOException;<br><span class="hljs-keyword">import</span> java.util.List;<br><br><span class="hljs-meta">@WebServlet(name = &quot;CommodityQueryAction&quot;, urlPatterns = &quot;/shop_center&quot;)</span><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">CommodityQueryAction</span> <span class="hljs-keyword">extends</span> <span class="hljs-title class_">HttpServlet</span> &#123;<br>    <span class="hljs-meta">@Override</span><br>    <span class="hljs-keyword">protected</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">doGet</span><span class="hljs-params">(HttpServletRequest req, HttpServletResponse resp)</span> <span class="hljs-keyword">throws</span> ServletException, IOException &#123;<br>        <span class="hljs-type">HttpSession</span> <span class="hljs-variable">session</span> <span class="hljs-operator">=</span> req.getSession();<br>        <span class="hljs-type">Page</span> <span class="hljs-variable">page</span> <span class="hljs-operator">=</span> (Page)session.getAttribute(<span class="hljs-string">&quot;pageInfo&quot;</span>);<br>        <span class="hljs-comment">// 若是第一次访问，则初始化page对象</span><br>        <span class="hljs-keyword">if</span>(page == <span class="hljs-literal">null</span>) &#123;<br>            <span class="hljs-comment">// 初始化</span><br>            page = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Page</span>();<br>            page.setPageCount(<span class="hljs-number">8</span>);<br>            page.setPageNow(<span class="hljs-number">0</span>);<br>            session.setAttribute(<span class="hljs-string">&quot;totalCommodity&quot;</span>, Service.commodity.queryAll().size());<br>        &#125;<br><br>        <span class="hljs-type">int</span> <span class="hljs-variable">tid</span> <span class="hljs-operator">=</span> -<span class="hljs-number">1</span>;<br>        <span class="hljs-comment">// 当前查询的商品类型，可能为空</span><br>        <span class="hljs-type">String</span> <span class="hljs-variable">tid_param</span> <span class="hljs-operator">=</span> req.getParameter(<span class="hljs-string">&quot;ctype&quot;</span>);<br>        <span class="hljs-comment">// session域名的nowType</span><br>        <span class="hljs-type">Integer</span> <span class="hljs-variable">tid_session</span> <span class="hljs-operator">=</span> (Integer) session.getAttribute(<span class="hljs-string">&quot;nowType&quot;</span>);<br>        <span class="hljs-comment">// 优先获取地址栏的参数</span><br>        <span class="hljs-keyword">if</span>(tid_param == <span class="hljs-literal">null</span> &amp;&amp; tid_session == <span class="hljs-literal">null</span>) &#123;<br>            tid = -<span class="hljs-number">1</span>;<br>        &#125;<br>        <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span>(tid_param != <span class="hljs-literal">null</span>) &#123; <span class="hljs-comment">// 重置页面为第一页</span><br>            page.setPageNow(<span class="hljs-number">0</span>);<br>            tid = Integer.parseInt(tid_param);<br>        &#125;<br>        <span class="hljs-keyword">else</span> tid = tid_session;<br><br>        <span class="hljs-comment">// 修改分页的属性</span><br>        page.setTotalRows(Service.commodity.countByTypeId(tid));<br>        page.setTotalPage(page.getTotalRows() / page.getPageCount());<br><br>        <span class="hljs-comment">// 根据商品类型分页查询商品数据</span><br>        List&lt;Commodity&gt; commodities = Service.commodity.query(page.getPageNow() * page.getPageCount(), page.getPageCount(), tid);<br>        <span class="hljs-comment">// 根据ID查询商品分类的名称和发布人的名称</span><br>        <span class="hljs-keyword">for</span> (Commodity c : commodities) &#123;<br>            c.setTypeName(Service.commodity.queryTypeById(c.getTid()));<br>            c.setUserName(Service.user.queryNameById(c.getUid()));<br>        &#125;<br><br>        <span class="hljs-comment">// 设置商品类型信息</span><br>        List&lt;CommodityType&gt; ctypes = Service.commodity.queryAllType();<br>        <span class="hljs-keyword">for</span> (CommodityType ctype : ctypes) &#123;<br>            <span class="hljs-comment">// 根据商品类型表的ID去统计商品表中同一个类型的个数</span><br>            ctype.setCount(Service.commodity.countByTypeId(ctype.getId()));<br>        &#125;<br>        <span class="hljs-comment">/* 保存页面信息 */</span><br>        session.setAttribute(<span class="hljs-string">&quot;pageInfo&quot;</span>, page);<br>        <span class="hljs-comment">/* 保存商城的商品类型到 session 域 */</span><br>        session.setAttribute(<span class="hljs-string">&quot;commodityTypes&quot;</span>, ctypes);<br>        session.setAttribute(<span class="hljs-string">&quot;nowType&quot;</span>, tid);<br>        <span class="hljs-comment">/* 保存商品的信息到 session 域 */</span><br>        session.setAttribute(<span class="hljs-string">&quot;commodityInfo&quot;</span>, commodities);<br>        session.setAttribute(<span class="hljs-string">&quot;nowPage&quot;</span>, <span class="hljs-number">0</span>);<br>        <span class="hljs-comment">// ... 重定向</span><br>        resp.sendRedirect(<span class="hljs-string">&quot;shop_center.jsp&quot;</span>);<br>    &#125;<br>&#125;<br><br></code></pre></td></tr></table></figure>

<h3 id="2-1-5-实现分页的JSP页面"><a href="#2-1-5-实现分页的JSP页面" class="headerlink" title="2.1.5 实现分页的JSP页面"></a>2.1.5 实现分页的JSP页面</h3><p>其中标签的类大部分是bootstrap5框架自带的样式，比如mb-5是框架的工具类，表示设置margin-bottom属性设置为等级5的一个大小，具体来说笔者并不清楚5是多少个单位，只知道mb-5可以给它一个不错的效果，能让这些商品卡片下方出现间隔。<br><code>商品卡片</code></p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><code class="hljs html"><span class="hljs-tag">&lt;<span class="hljs-name">c:forEach</span> <span class="hljs-attr">items</span>=<span class="hljs-string">&quot;$&#123;sessionScope.commodityInfo&#125;&quot;</span> <span class="hljs-attr">var</span>=<span class="hljs-string">&quot;cd&quot;</span>&gt;</span><br>     <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;col-3 mb-5&quot;</span>&gt;</span><br>         <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;card-commodity&quot;</span>&gt;</span><br>             <span class="hljs-tag">&lt;<span class="hljs-name">img</span> <span class="hljs-attr">src</span>=<span class="hljs-string">&quot;$&#123;cd.img&#125;&quot;</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;card-img-top&quot;</span>&gt;</span><br>             <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;card-body&quot;</span>&gt;</span><br>                 <span class="hljs-tag">&lt;<span class="hljs-name">h5</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;card-title text-center&quot;</span>&gt;</span>￥$&#123;cd.price&#125;<span class="hljs-tag">&lt;/<span class="hljs-name">h5</span>&gt;</span><br>                 <span class="hljs-tag">&lt;<span class="hljs-name">h6</span>&gt;</span>商品名称: <span class="hljs-tag">&lt;<span class="hljs-name">span</span> <span class="hljs-attr">style</span>=<span class="hljs-string">&quot;color: blue&quot;</span>&gt;</span>$&#123;cd.name&#125;<span class="hljs-tag">&lt;/<span class="hljs-name">span</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">h6</span>&gt;</span><br>                 <span class="hljs-tag">&lt;<span class="hljs-name">h6</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;card-text&quot;</span> <span class="hljs-attr">href</span>=<span class="hljs-string">&quot;#&quot;</span>&gt;</span>商品类型: $&#123;cd.typeName&#125;<span class="hljs-tag">&lt;/<span class="hljs-name">h6</span>&gt;</span><br>                 <span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;card-text&quot;</span> <span class="hljs-attr">href</span>=<span class="hljs-string">&quot;#&quot;</span>&gt;</span>商品描述: $&#123;cd.info&#125;<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span><br>                 <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>商家: <span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">&quot;#&quot;</span>&gt;</span>$&#123;cd.userName&#125;<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span><br>             <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br>         <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br>     <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br> <span class="hljs-tag">&lt;/<span class="hljs-name">c:forEach</span>&gt;</span><br></code></pre></td></tr></table></figure>
<p><code>分页按钮</code></p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><code class="hljs html"><span class="hljs-tag">&lt;<span class="hljs-name">c:forEach</span> <span class="hljs-attr">begin</span>=<span class="hljs-string">&quot;$&#123;sessionScope.pageInfo.pageNow&#125;&quot;</span> <span class="hljs-attr">end</span>=<span class="hljs-string">&quot;$&#123;sessionScope.pageInfo.pageNow + 5&#125;&quot;</span> <span class="hljs-attr">var</span>=<span class="hljs-string">&quot;x&quot;</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">c:if</span> <span class="hljs-attr">test</span>=<span class="hljs-string">&quot;$&#123;x &lt;= sessionScope.pageInfo.totalPage&#125;&quot;</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">li</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;page-item&quot;</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;page_a page-link d-block&quot;</span> <span class="hljs-attr">href</span>=<span class="hljs-string">&quot;/shop?page=$&#123;x+1&#125;&quot;</span>&gt;</span>$&#123;x+1&#125;<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span><br>        <span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">c:if</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">c:forEach</span>&gt;</span><br></code></pre></td></tr></table></figure>

<h2 id="2-2-购买窗口商品卡片的弹窗-Modal"><a href="#2-2-购买窗口商品卡片的弹窗-Modal" class="headerlink" title="2.2 购买窗口商品卡片的弹窗 Modal"></a>2.2 购买窗口商品卡片的弹窗 Modal</h2><p>弹窗效果是使用 Bootstrap5框架的 Modal组件<br><img src="https://img-blog.csdnimg.cn/cdef32be35794a5ea3faf5f173fdf300.png#pic_center" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<p><a target="_blank" rel="noopener" href="https://v5.bootcss.com/docs/components/modal/">Bootstrap5 Modal官方文档</a><br><img src="https://img-blog.csdnimg.cn/ec8a849dbb524da4b2ca1d3501354a7a.png#pic_center" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"><br>实现的HTML代码:</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><code class="hljs html"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;modal fade&quot;</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;staticBackdrop&quot;</span> <span class="hljs-attr">data-bs-backdrop</span>=<span class="hljs-string">&quot;static&quot;</span> <span class="hljs-attr">data-bs-keyboard</span>=<span class="hljs-string">&quot;false&quot;</span> <span class="hljs-attr">tabindex</span>=<span class="hljs-string">&quot;-1&quot;</span> <span class="hljs-attr">aria-labelledby</span>=<span class="hljs-string">&quot;staticBackdropLabel&quot;</span> <span class="hljs-attr">aria-hidden</span>=<span class="hljs-string">&quot;true&quot;</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;modal-dialog&quot;</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;modal-content&quot;</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;modal-header&quot;</span>&gt;</span><br>                <span class="hljs-tag">&lt;<span class="hljs-name">h5</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;modal-title&quot;</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;staticBackdropLabel&quot;</span>&gt;</span>购买商品<span class="hljs-tag">&lt;/<span class="hljs-name">h5</span>&gt;</span><br>                <span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;button&quot;</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;btn-close&quot;</span> <span class="hljs-attr">data-bs-dismiss</span>=<span class="hljs-string">&quot;modal&quot;</span> <span class="hljs-attr">aria-label</span>=<span class="hljs-string">&quot;Close&quot;</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span><br>            <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;modal-body&quot;</span>&gt;</span><br>                是否确认购买？<br>            <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;modal-footer&quot;</span>&gt;</span><br>                <span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;button&quot;</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;btn btn-danger&quot;</span> <span class="hljs-attr">data-bs-dismiss</span>=<span class="hljs-string">&quot;modal&quot;</span>&gt;</span>我再想想<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span><br>                <span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;button&quot;</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;btn btn-success&quot;</span>&gt;</span>确认购买<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span><br>            <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br>        <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br></code></pre></td></tr></table></figure>

<h2 id="2-3-JQuery表单提交插件-amp-发布商品"><a href="#2-3-JQuery表单提交插件-amp-发布商品" class="headerlink" title="2.3 JQuery表单提交插件 &amp; 发布商品"></a>2.3 JQuery表单提交插件 &amp; 发布商品</h2><h3 id="2-3-1-安装JQuery插件"><a href="#2-3-1-安装JQuery插件" class="headerlink" title="2.3.1 安装JQuery插件"></a>2.3.1 安装JQuery插件</h3><p>下载JQuery 上传文件的官方插件</p>
<p><a target="_blank" rel="noopener" href="https://plugins.jquery.com/blueimp-file-upload/">官方地址</a><br><img src="https://img-blog.csdnimg.cn/ade900ca37844e1fa53c878e56c0f8ff.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"><br>除了下载源文件，还有一种更方便的办法，直接访问这个网址 <a target="_blank" rel="noopener" href="https://github.com/jquery-form/form/edit/master/dist/jquery.form.min.js">JQuery - form插件 源代码</a><br>将里面的代码复制，然后在web项目的js文件夹里新建一个js脚本，复制进去后，在HTML页面添加即可。</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs html"><span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;text/javascript&quot;</span> <span class="hljs-attr">src</span>=<span class="hljs-string">&quot;js/jquery-form-min.js&quot;</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span><br></code></pre></td></tr></table></figure>
<h3 id="2-3-2-ajaxSubmit请求"><a href="#2-3-2-ajaxSubmit请求" class="headerlink" title="2.3.2 ajaxSubmit请求"></a>2.3.2 ajaxSubmit请求</h3><p>发布商品是通过AJAX请求来实现的，插件提供的ajaxSubmit方法可以直接提交整个表单的数据，因为笔者的表单按钮是在表单外面的，没有在表单里，所以可以通过这种方式来提交表单。</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><code class="hljs javascript">$(<span class="hljs-variable language_">document</span>).<span class="hljs-title function_">ready</span>(<span class="hljs-keyword">function</span> (<span class="hljs-params"></span>)&#123;<br>    <span class="hljs-comment">// $(&quot;#form-publish-img&quot;).on(function ()&#123;</span><br>    <span class="hljs-comment">// 上传图片</span><br>    $(<span class="hljs-string">&quot;#form-publish-img&quot;</span>).<span class="hljs-title function_">bind</span>(<span class="hljs-string">&quot;input propertychange&quot;</span>,<span class="hljs-keyword">function</span>(<span class="hljs-params">event</span>)&#123;<br>        <span class="hljs-keyword">var</span> reads = <span class="hljs-keyword">new</span> <span class="hljs-title class_">FileReader</span>();<br>        <span class="hljs-keyword">var</span> f = $(<span class="hljs-variable language_">this</span>).<span class="hljs-title function_">get</span>(<span class="hljs-number">0</span>).<span class="hljs-property">files</span>[<span class="hljs-number">0</span>];<br>        <span class="hljs-keyword">var</span> rep = <span class="hljs-regexp">/jpeg|png|gif|bmp|jpg/ig</span>;<br>        <span class="hljs-keyword">var</span> gstyle = f.<span class="hljs-property">type</span>.<span class="hljs-title function_">split</span>(<span class="hljs-string">&quot;/&quot;</span>)[<span class="hljs-number">1</span>];<br><br>        <span class="hljs-keyword">if</span>(rep.<span class="hljs-title function_">test</span>(gstyle))&#123;<br>             reads.<span class="hljs-title function_">readAsDataURL</span>(f);<br>             reads.<span class="hljs-property">onload</span> = <span class="hljs-keyword">function</span>(<span class="hljs-params">e</span>) &#123;<br>                     $(<span class="hljs-string">&quot;#form-publish-img-show&quot;</span>).<span class="hljs-title function_">attr</span>(<span class="hljs-string">&quot;src&quot;</span>,<span class="hljs-variable language_">this</span>.<span class="hljs-property">result</span>)<br>                 &#125;;<br>        &#125;<span class="hljs-keyword">else</span>&#123;<br>             <span class="hljs-title function_">alert</span>(<span class="hljs-string">&quot;图片格式不正确，请上传 jpeg|png|gif|bmp 格式的图片&quot;</span>)<br>        &#125;<br>    &#125;)<br><br>    $(<span class="hljs-string">&quot;#form-button&quot;</span>).<span class="hljs-title function_">click</span>(<span class="hljs-keyword">function</span> (<span class="hljs-params"></span>)&#123;<br><br>        $(<span class="hljs-string">&quot;#form-publish&quot;</span>).<span class="hljs-title function_">ajaxSubmit</span>(&#123;<br>            <span class="hljs-comment">//?commodityName=&#x27; + $(&quot;#input-commodity-name&quot;).val()</span><br>            <span class="hljs-attr">type</span>: <span class="hljs-string">&#x27;post&#x27;</span>,<br>            <span class="hljs-attr">enctype</span>: <span class="hljs-string">&#x27;multipart/form-data&#x27;</span>,<br>            <span class="hljs-attr">url</span>: <span class="hljs-string">&#x27;/publish&#x27;</span>,<br>            <span class="hljs-attr">success</span>: <span class="hljs-keyword">function</span> (<span class="hljs-params">data</span>)&#123;<br>                <span class="hljs-title function_">alert</span>(<span class="hljs-string">&#x27;提交成功!&#x27;</span> + data)<br>            &#125;<br>        &#125;)<br>    &#125;);<br>&#125;);<br><br></code></pre></td></tr></table></figure>

<h3 id="2-3-3-servlet保存商品信息"><a href="#2-3-3-servlet保存商品信息" class="headerlink" title="2.3.3 servlet保存商品信息"></a>2.3.3 servlet保存商品信息</h3><p><code>CommodityPublishAction.java</code></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">package</span> com.uni.action;<br><br><br><span class="hljs-keyword">import</span> com.uni.entity.Commodity;<br><span class="hljs-keyword">import</span> com.uni.entity.User;<br><br><span class="hljs-keyword">import</span> com.uni.service.Service;<br><span class="hljs-keyword">import</span> com.uni.utils.FileUtil;<br><br><span class="hljs-keyword">import</span> javax.servlet.ServletException;<br><span class="hljs-keyword">import</span> javax.servlet.annotation.WebServlet;<br><span class="hljs-keyword">import</span> javax.servlet.http.HttpServlet;<br><span class="hljs-keyword">import</span> javax.servlet.http.HttpServletRequest;<br><span class="hljs-keyword">import</span> javax.servlet.http.HttpServletResponse;<br><span class="hljs-keyword">import</span> java.io.*;<br><br><span class="hljs-meta">@WebServlet(name = &quot;CommodityPublishAction&quot;, urlPatterns = &quot;/publish&quot;)</span><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">CommodityPublishAction</span> <span class="hljs-keyword">extends</span> <span class="hljs-title class_">HttpServlet</span> &#123;<br><br>    <span class="hljs-meta">@Override</span><br>    <span class="hljs-keyword">protected</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">doGet</span><span class="hljs-params">(HttpServletRequest req, HttpServletResponse resp)</span> <span class="hljs-keyword">throws</span> ServletException, IOException &#123;<br>        resp.getWriter().write(<span class="hljs-string">&quot;不允许使用Get方式请求该路径&quot;</span>);<br>    &#125;<br><br>    <span class="hljs-meta">@Override</span><br>    <span class="hljs-keyword">protected</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">doPost</span><span class="hljs-params">(HttpServletRequest req, HttpServletResponse resp)</span> <span class="hljs-keyword">throws</span> ServletException, IOException &#123;<br>        <span class="hljs-comment">// 【获取用户以及用户填写的商品信息】</span><br>        <span class="hljs-type">User</span> <span class="hljs-variable">user</span> <span class="hljs-operator">=</span> (User) req.getSession().getAttribute(<span class="hljs-string">&quot;userInfo&quot;</span>);<br>        <span class="hljs-comment">// 【新建商品对象】</span><br>        <span class="hljs-type">Commodity</span> <span class="hljs-variable">commodity</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Commodity</span>();<br>        commodity.setUid(user.getId());<br>        commodity.setName(FileUtil.read(req, <span class="hljs-string">&quot;commodity-name&quot;</span>));<br>        commodity.setInfo(FileUtil.read(req, <span class="hljs-string">&quot;commodity-info&quot;</span>));<br>        commodity.setTid(Integer.parseInt(FileUtil.read(req, <span class="hljs-string">&quot;commodityType&quot;</span>)));<br>        commodity.setPrice(Float.parseFloat(FileUtil.read(req, <span class="hljs-string">&quot;commodity-price&quot;</span>)));<br><span class="hljs-comment">//        System.out.println(&quot;读取表单信息后&quot; + commodity);</span><br>        <span class="hljs-comment">// 【获取商品名称】</span><br>        <span class="hljs-type">String</span> <span class="hljs-variable">path0</span> <span class="hljs-operator">=</span> req.getServletContext().getRealPath( <span class="hljs-string">&quot;./&quot;</span>) + File.separator + user.getName() + File.separator;<br>        <span class="hljs-type">String</span> <span class="hljs-variable">path1</span> <span class="hljs-operator">=</span> commodity.getName() + <span class="hljs-string">&quot;.jpg&quot;</span>;<br>        <span class="hljs-keyword">if</span>(FileUtil.saveFile(req, path0, path1)==<span class="hljs-literal">true</span>) &#123;<br>            commodity.setImg(user.getName() + File.separator + path1);<br>            Service.commodity.insertCommodity(commodity);<br>            resp.getWriter().write(<span class="hljs-string">&quot;操作成功&quot;</span>);<br>        &#125; <span class="hljs-keyword">else</span>&#123;<br>            resp.getWriter().write(<span class="hljs-string">&quot;操作失败&quot;</span>);<br>        &#125;<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<p><code>FileUtil.java</code></p>
<p>此类负责根据请求对象，去获取请求的表单信息的某个input标签，比如存储图片的input标签用于saveFile方法中，通过IO流处理，将图片保存到服务器本地，而read方法则是获取表单属性中某一个name值为name的标签内容，通过这种方式可直接获取用户在表单填写的商品信息，比如商品名称、价格之类的。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">package</span> com.uni.utils;<br><br><span class="hljs-keyword">import</span> javax.servlet.ServletException;<br><span class="hljs-keyword">import</span> javax.servlet.http.HttpServletRequest;<br><span class="hljs-keyword">import</span> javax.servlet.http.Part;<br><span class="hljs-keyword">import</span> java.io.*;<br><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">FileUtil</span> &#123;<br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> String <span class="hljs-title function_">read</span><span class="hljs-params">(HttpServletRequest req, String name)</span>&#123;<br>        <span class="hljs-type">StringBuffer</span> <span class="hljs-variable">result</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">StringBuffer</span>();<br>        <span class="hljs-keyword">try</span> &#123;<br>            <span class="hljs-type">Part</span> <span class="hljs-variable">part</span> <span class="hljs-operator">=</span> req.getPart(name);<br>            <span class="hljs-type">InputStream</span> <span class="hljs-variable">is</span> <span class="hljs-operator">=</span> part.getInputStream();<br>            <span class="hljs-type">InputStreamReader</span> <span class="hljs-variable">ris</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">InputStreamReader</span>(is, <span class="hljs-string">&quot;UTF-8&quot;</span>);<br>            <span class="hljs-type">BufferedReader</span> <span class="hljs-variable">bfr</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">BufferedReader</span>(ris);<br>            <span class="hljs-type">String</span> <span class="hljs-variable">temp</span> <span class="hljs-operator">=</span> <span class="hljs-string">&quot;&quot;</span>;<br>            <span class="hljs-keyword">if</span> ((temp = bfr.readLine()) != <span class="hljs-literal">null</span>)<br>                result.append(temp);<br>            bfr.close();<br>            ris.close();<br>            is.close();<br>        &#125; <span class="hljs-keyword">catch</span> (IOException e) &#123;<br>            e.printStackTrace();<br>        &#125; <span class="hljs-keyword">catch</span> (ServletException e) &#123;<br>            e.printStackTrace();<br>        &#125;<br>        <span class="hljs-keyword">return</span> result.toString();<br>    &#125;<br><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-type">boolean</span> <span class="hljs-title function_">saveFile</span><span class="hljs-params">(HttpServletRequest req, String path0, String path1)</span>&#123;<br>        <span class="hljs-type">String</span> <span class="hljs-variable">path</span> <span class="hljs-operator">=</span> path0 + path1;<br>        <span class="hljs-comment">// 如果目录不存在则创建</span><br>        <span class="hljs-type">File</span> <span class="hljs-variable">uploadDir</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">File</span>(path0);<br>        <span class="hljs-keyword">try</span>&#123;<br>            <span class="hljs-keyword">if</span> (!uploadDir.exists()) uploadDir.mkdir();<br>            <span class="hljs-type">Part</span> <span class="hljs-variable">part</span> <span class="hljs-operator">=</span> req.getPart(<span class="hljs-string">&quot;form-publish-img&quot;</span>);<br>            <span class="hljs-type">InputStream</span> <span class="hljs-variable">is</span> <span class="hljs-operator">=</span> part.getInputStream();<br>            <span class="hljs-type">FileOutputStream</span> <span class="hljs-variable">fos</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">FileOutputStream</span>(path);<br>            <span class="hljs-type">byte</span>[] bty = <span class="hljs-keyword">new</span> <span class="hljs-title class_">byte</span>[<span class="hljs-number">1024</span> * <span class="hljs-number">10</span>];<br>            <span class="hljs-type">int</span> <span class="hljs-variable">length</span> <span class="hljs-operator">=</span><span class="hljs-number">0</span>;<br>            <span class="hljs-keyword">while</span>((length=is.read(bty))!=-<span class="hljs-number">1</span>)&#123;<br>                fos.write(bty,<span class="hljs-number">0</span>,length);<br>            &#125;<br>            fos.close();<br>            is.close();<br>        &#125; <span class="hljs-keyword">catch</span> (Exception e)&#123;<br>            e.printStackTrace();<br>            <span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>;<br>        &#125;<br>        <span class="hljs-keyword">return</span> <span class="hljs-literal">true</span>;<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>
<h1 id="3-总结"><a href="#3-总结" class="headerlink" title="3. 总结"></a>3. 总结</h1><hr>
<p>在实现图片保存功能时，笔者在各大博客网站查询了资料，有说使用一个servlet插件，貌似是uploadFile，但是笔者在尝试时，遍历表单的信息，结果只有两个input标签，之后改成servlet原生的对象 Part 则可以正常读取。</p>
<p>整个过程还是比较复杂的，通过这两天的学习，初次使用了JQuery的form表单插件，熟悉了分页查询的实现过程。</p>
<h1 id="下一部分"><a href="#下一部分" class="headerlink" title="下一部分"></a>下一部分</h1><p><a target="_blank" rel="noopener" href="https://blog.csdn.net/Unirithe/article/details/122121845">Web期末课程设计v1.0（五） | 大学生闲置物品交易系统 | 模拟网站埋点实现用户浏览记录的统计与显示</a></p>

                
              </div>
            
            <hr/>
            <div>
              <div class="post-metas my-3">
  
    <div class="post-meta mr-3 d-flex align-items-center">
      <i class="iconfont icon-category"></i>
      

<span class="category-chains">
  
  
    
      <span class="category-chain">
        
  <a href="/categories/%E9%A1%B9%E7%9B%AE%E5%AE%9E%E6%88%98-%E8%B4%AD%E7%89%A9%E7%B3%BB%E7%BB%9Fv1-0/" class="category-chain-item">项目实战:购物系统v1.0</a>
  
  

      </span>
    
  
</span>

    </div>
  
  
    <div class="post-meta">
      <i class="iconfont icon-tags"></i>
      
        <a href="/tags/JavaWeb/">#JavaWeb</a>
      
        <a href="/tags/%E9%A1%B9%E7%9B%AE%E5%AE%9E%E6%88%98-%E8%B4%AD%E7%89%A9%E7%B3%BB%E7%BB%9Fv1-0/">#项目实战:购物系统v1.0</a>
      
    </div>
  
</div>


              
  

  <div class="license-box my-3">
    <div class="license-title">
      <div>Web项目实战 _ 购物系统v1.0 _ 开发记录（四） _ 实现JQuery图片上传 _ 使用JQ插件提交AJAX请求实现发布商品并保存到数据库 _ 商品分类分页显示</div>
      <div>http://example.com/2021/12/23/Web项目实战 _ 购物系统v1.0 _ 开发记录（四） _ 实现JQuery图片上传 _ 使用JQ插件提交AJAX请求实现发布商品并保存到数据库 _ 商品分类分页显示/</div>
    </div>
    <div class="license-meta">
      
        <div class="license-meta-item">
          <div>作者</div>
          <div>John Doe</div>
        </div>
      
      
        <div class="license-meta-item license-meta-date">
          <div>发布于</div>
          <div>2021年12月23日</div>
        </div>
      
      
      <div class="license-meta-item">
        <div>许可协议</div>
        <div>
          
            
            
              <a target="_blank" href="https://creativecommons.org/licenses/by/4.0/">
              <span class="hint--top hint--rounded" aria-label="BY - 署名">
                <i class="iconfont icon-by"></i>
              </span>
              </a>
            
          
        </div>
      </div>
    </div>
    <div class="license-icon iconfont"></div>
  </div>



              
                <div class="post-prevnext my-3">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/2021/12/23/%E5%A4%A7%E6%95%B0%E6%8D%AE%E5%BA%94%E7%94%A8%E6%8A%80%E6%9C%AF%20_%20%E4%BD%BF%E7%94%A8%20Docker%E6%90%AD%E5%BB%BA%20Flink%20%E9%9B%86%E7%BE%A4%20_%20FlinkStreaming%E5%AE%9E%E6%97%B6%E6%95%B0%E6%8D%AE%E6%B5%81%E5%A4%84%E7%90%86%E7%A8%8B%E5%BA%8F%E5%AE%9E%E7%8E%B0%E8%AF%8D%E9%A2%91%E7%BB%9F%E8%AE%A1/" title="大数据应用技术_使用 Docker搭建 Flink 集群_FlinkStreaming实时数据流处理程序实现词频统计">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">大数据应用技术_使用 Docker搭建 Flink 集群_FlinkStreaming实时数据流处理程序实现词频统计</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/2021/12/21/Web%E9%A1%B9%E7%9B%AE%E5%AE%9E%E6%88%98%20_%20%E8%B4%AD%E7%89%A9%E7%B3%BB%E7%BB%9Fv1.0%20_%20%E5%BC%80%E5%8F%91%E8%AE%B0%E5%BD%95%EF%BC%88%E4%B8%89%EF%BC%89%20_%20%E5%9F%BA%E4%BA%8EBootstrap%E8%AE%BE%E8%AE%A1%E5%95%86%E5%93%81%E9%A1%B5%E9%9D%A2%E3%80%81%E4%BD%BF%E7%94%A8JQuery%E5%AE%9E%E7%8E%B0%E4%B8%8A%E4%BC%A0%E6%96%87%E4%BB%B6%E5%B9%B6%E6%98%BE%E7%A4%BA%E5%88%B0%E5%9B%BE%E7%89%87%E7%BB%84%E4%BB%B6%E3%80%81%E4%BD%BF%E7%94%A8JSTL%E6%A0%87%E7%AD%BE%E6%98%BE%E7%A4%BA%E6%95%B0%E6%8D%AE%E5%BA%93%E7%9A%84%E5%86%85%E5%AE%B9%E5%88%B0%E6%97%A0%E5%BA%8F%E5%88%97%E8%A1%A8/" title="Web项目实战 _ 购物系统v1.0 _ 开发记录（三） _ 基于Bootstrap设计商品页面、使用JQuery实现上传文件并显示到图片组件、使用JSTL标签显示数据库的内容到无序列表">
                        <span class="hidden-mobile">Web项目实战 _ 购物系统v1.0 _ 开发记录（三） _ 基于Bootstrap设计商品页面、使用JQuery实现上传文件并显示到图片组件、使用JSTL标签显示数据库的内容到无序列表</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
          </article>
        </div>
      </div>
    </div>

    <div class="side-col d-none d-lg-block col-lg-2">
      
  <aside class="sidebar" style="margin-left: -1rem">
    <div id="toc">
  <p class="toc-header"><i class="iconfont icon-list"></i>&nbsp;目录</p>
  <div class="toc-body" id="toc-body"></div>
</div>



  </aside>


    </div>
  </div>
</div>





  



  



  



  



  







    

    
      <a id="scroll-top-button" aria-label="TOP" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v" for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>

    

    
  </main>

  <footer>
    <div class="footer-inner">
  
    <div class="footer-content">
       <a href="https://hexo.io" target="_blank" rel="nofollow noopener"><span>Hexo</span></a> <i class="iconfont icon-love"></i> <a href="https://github.com/fluid-dev/hexo-theme-fluid" target="_blank" rel="nofollow noopener"><span>Fluid</span></a> 
    </div>
  
  
  
  
</div>

  </footer>

  <!-- Scripts -->
  
  <script  src="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://lib.baomitu.com/jquery/3.6.0/jquery.min.js" ></script>
<script  src="https://lib.baomitu.com/twitter-bootstrap/4.6.1/js/bootstrap.min.js" ></script>
<script  src="/js/events.js" ></script>
<script  src="/js/plugins.js" ></script>


  <script  src="https://lib.baomitu.com/typed.js/2.0.12/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var subtitle = document.getElementById('subtitle');
      if (!subtitle || !typing) {
        return;
      }
      var text = subtitle.getAttribute('data-typed-text');
      
        typing(text);
      
    })(window, document);
  </script>




  
    <script  src="/js/img-lazyload.js" ></script>
  




  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/tocbot/4.18.2/tocbot.min.js', function() {
    var toc = jQuery('#toc');
    if (toc.length === 0 || !window.tocbot) { return; }
    var boardCtn = jQuery('#board-ctn');
    var boardTop = boardCtn.offset().top;

    window.tocbot.init({
      tocSelector     : '#toc-body',
      contentSelector : '.markdown-body',
      headingSelector : CONFIG.toc.headingSelector || 'h1,h2,h3,h4,h5,h6',
      linkClass       : 'tocbot-link',
      activeLinkClass : 'tocbot-active-link',
      listClass       : 'tocbot-list',
      isCollapsedClass: 'tocbot-is-collapsed',
      collapsibleClass: 'tocbot-is-collapsible',
      collapseDepth   : CONFIG.toc.collapseDepth || 0,
      scrollSmooth    : true,
      headingsOffset  : -boardTop
    });
    if (toc.find('.toc-list-item').length > 0) {
      toc.css('visibility', 'visible');
    }
  });
</script>


  <script src=https://lib.baomitu.com/clipboard.js/2.0.10/clipboard.min.js></script>

  <script>Fluid.plugins.codeWidget();</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/anchor-js/4.3.1/anchor.min.js', function() {
    window.anchors.options = {
      placement: CONFIG.anchorjs.placement,
      visible  : CONFIG.anchorjs.visible
    };
    if (CONFIG.anchorjs.icon) {
      window.anchors.options.icon = CONFIG.anchorjs.icon;
    }
    var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
    var res = [];
    for (var item of el) {
      res.push('.markdown-body > ' + item.trim());
    }
    if (CONFIG.anchorjs.placement === 'left') {
      window.anchors.options.class = 'anchorjs-link-left';
    }
    window.anchors.add(res.join(', '));
  });
</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.js', function() {
    Fluid.plugins.fancyBox();
  });
</script>


  <script>Fluid.plugins.imageCaption();</script>

  <script  src="/js/local-search.js" ></script>





<!-- 主题的启动项，将它保持在最底部 -->
<!-- the boot of the theme, keep it at the bottom -->
<script  src="/js/boot.js" ></script>


  

  <noscript>
    <div class="noscript-warning">博客在允许 JavaScript 运行的环境下浏览效果更佳</div>
  </noscript>
</body>
</html>
